package com.xs.ahoi.hcxs.ui.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnItemClick;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.dao.ConfigDao;
import com.xs.ahoi.hcxs.utils.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 翻页动画弹窗页面
 */
public class DialogPageStyle extends BaseDialog {

    @BindView(R.id.dialogListView)
    public ListView dialogListView;

    private ConfigDao configDao;
    private List<Map<String, Object>> datas;
    private Map<String, Object> oneData;
    private PageStyleAdapter adapter;
    private PageStyleListener pageStyleListener;

    private String[] styleArray = new String[]{"仿真", "滑动", "覆盖", "无动画"};

    public DialogPageStyle(@NonNull Context context) {
        super(context, R.style.common_dialog);
    }

    @Override
    protected void onStart() {
        super.onStart();
        initData();
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_page_style;
    }

    @Override
    protected int showPosition() {
        return Gravity.CENTER;
    }

    @Override
    protected void initListener() {

    }

    // 初始化
    @Override
    protected void initData() {
        configDao = ConfigDao.getInstance();

        // 获取缓存中保存的翻页动画
        int defaultStyle = configDao.getPageStyle();

        // 初始化列表数据
        datas = new ArrayList<>();
        for (int i = 0; i < styleArray.length; i++) {
            String style = styleArray[i];
            oneData = new HashMap<>();
            oneData.put("name", style);
            if (i == defaultStyle) {
                oneData.put("select", "1");
            } else {
                oneData.put("select", "0");
            }
            datas.add(oneData);
        }

        // 绑定adapter
        adapter = new PageStyleAdapter(getContext(), datas, R.layout.listview_item_dialog_common, new String[]{"name"}, new int[]{R.id.tvName});
        dialogListView.setAdapter(adapter);
    }

    @OnItemClick({R.id.dialogListView})
    public void onItemClick(int position) {

        Map<String, Object> map;
        for (int i = 0; i < datas.size(); i++) {
            map = datas.get(i);
            if (i == position) {
                map.put("select", "1");
            } else {
                map.put("select", "0");
            }
        }
        adapter.notifyDataSetChanged();
    }

    @OnClick({R.id.tvDialogOk, R.id.tvDialogCancel})
    public void onClick(View v) {
        switch (v.getId()) {
            // 确认
            case R.id.tvDialogOk:
                pageStyleListener.changePageStyle(getSelectedItem());
                super.dismiss();
                break;
            // 取消
            case R.id.tvDialogCancel:
                super.dismiss();
                break;
        }
    }

    // 获取选中的项
    private int getSelectedItem() {
        Map<String, Object> map;
        for (int i = 0; i < datas.size(); i++) {
            map = datas.get(i);
            if (ObjectUtils.toInteger(map.get("select")) == 1) {
                return i;
            }
        }
        return 0;
    }

    public void setPageStyleListener(PageStyleListener pageStyleListener) {
        this.pageStyleListener = pageStyleListener;
    }


    public interface PageStyleListener {
        void changePageStyle(int pageStyle);
    }


    //**********************************************************
    //********************get set 方法**************************
    //**********************************************************

    // 自定义SimpleAdapter
    private class PageStyleAdapter extends SimpleAdapter {

        private PageStyleAdapter(Context context, List<? extends Map<String, Object>> datas, int resource, String[] from, int[] to) {
            super(context, datas, resource, from, to);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);

            // 将已选中的编码设置为红色
            Map<String, Object> data = getItem(position);
            TextView tvCharset = view.findViewById(R.id.tvName);
            ImageView ivCheck = view.findViewById(R.id.ivCheck);
            if (ObjectUtils.toInteger(data.get("select")) == 1) {
                tvCharset.setTextColor(ContextCompat.getColor(getContext(), R.color.colorRed));
                ivCheck.setVisibility(View.VISIBLE);
            } else {
                tvCharset.setTextColor(ContextCompat.getColor(getContext(), R.color.colorBlack));
                ivCheck.setVisibility(View.GONE);
            }
            return view;
        }

        @Override
        public Map<String, Object> getItem(int position) {
            return datas.get(position);
        }
    }
}
